/**
 * 
 */
package controller.xml;

import model.BasicParameter;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;


/**
 * Use to parse a {@link BasicParameter} in the exported session in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class BasicParameterXMLHandler extends XMLHandler {
	private Integer					timeout	= null;
	private BasicParameterXMLStatus	status	= BasicParameterXMLStatus.NONE;

	public BasicParameterXMLHandler() {
	}

	@Override
	public boolean characters2(char[] ch, int start, int length) throws SAXException {

		switch (status) {
		case NONE:
			throw new SAXException("not supposed to receive character in this status");
		default:
			return super.characters2(ch, start, length);
		}
	}

	@Override
	public boolean endDocument2() throws SAXException {
		return false;
	}

	@Override
	public boolean endElement2(String uri, String localName, String qName) throws SAXException {
		switch (status) {
		case NONE:
			return false;

		case TIMEOUT:
			if (qName.equals(XMLBaliseFormat.BASIC_PARAMETER_TIMEOUT))
			{// ferme timeout
				setTimeout();
				status = BasicParameterXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of TIMEOUT was expected");
		}

		// attend une fin de balise qui existe pas...
		throw new SAXException("an other end of element was expected");
	}

	@Override
	public boolean startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if (status != BasicParameterXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.BASIC_PARAMETER_TIMEOUT))
		{// balise timeout
			status = BasicParameterXMLStatus.TIMEOUT;
			return true;
		}

		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}

	/**
	 * Set the timeout
	 * 
	 * @throws SAXException
	 */
	private void setTimeout() throws SAXException {
		try
		{
			timeout = Integer.valueOf(buffer.toString());
		} catch (Exception e)
		{
			throw new SAXException("bad integer value for TIMEOUT");
		}
	}

	/**
	 * Return the timeout.
	 * 
	 * @return the timeout
	 */
	public Integer getTimeout() {
		return timeout;
	}


}
